import { combineReducers } from 'redux'
import { routerReducer as router } from 'react-router-redux'

import system from './modules/system'

function createReducer(asyncReducers) {
  return combineReducers({
    ...asyncReducers,
    system,
    router,
  })
}

function injectReducer(store, { key, reducer }) {
  if (Reflect.has(store.asyncReducers, key)) return

  store.asyncReducers[key] = reducer // eslint-disable-line no-param-reassign
  store.replaceReducer(createReducer(store.asyncReducers))
}

export { createReducer, injectReducer }
